<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fn="http://www.w3.org/2005/02/xpath-functions">
    <xsl:param name="DOTNET2"/>


	<xsl:template match="Class">
		<xsl:variable name="Name" select="Name"/>
        <xsl:variable name="HeaderInfo" select="HeaderInfo/ProjectName"/>
// GENNIT Castle ActiveRecord http://www.gennit.com
//
// Project: <xsl:value-of select="HeaderInfo/ProjectName"/>
//
// **************************************************************
// IMPORTANT BIT:
//
// This source code is is owned by you. No copyright is held by
// GENNIT. However, please note the disclaimer below
// **************************************************************
//
// GENNIT refers to the GENNIT website and anyone affiliated
// with the GENNIT system. Including but not limited to
// the administrator and service provider.
//
// Unless agreed to in writing, source code is distributed on an 
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. GENNIT are not responsible for 
// your use/misuse of this code.

// When you use GENNIT code, you do so at your own risk and 
// GENNIT is not responsible for the accuracy, 
// reliability or functionality of the code. GENNIT expressly 
// disclaims any potential liability associated with use of 
// this generated code.



using System;
using System.Configuration;

using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework;

using System.Collections.Generic;

using System.Globalization;

using NUnit.Framework;

namespace <xsl:value-of select="HeaderInfo/ProjectName"/>.Dal.Tests 
{

	<xsl:if test="IsComposite='false' and IsNested='false'">
	[TestFixture]
	public class <xsl:value-of select="Name"/>DatabaseTest : NUnitBase
	{
		
		<![CDATA[/// <summary>]]>
		/// Test to see if the database contains any records of this ActiveRecord <xsl:value-of select="Name"/> objects.
		/// This test is explicit as it is not always likely for the database to contain records.
		<![CDATA[/// </summary>]]>
		[Test(Description="Tests to see if there are any records for <xsl:value-of select="Name"/>")]
		[Explicit]
		public static void HasRecords ()
		{
			int records = <xsl:value-of select="HeaderInfo/ProjectName"/>.Dal.<xsl:value-of select="Name"/>.FindAll ().Length;

			Assert.IsTrue ( records > 0 );
		}
		
		<![CDATA[/// <summary>]]>
		/// Creates an <xsl:value-of select="Name"/> object and populates the data within it.
		/// All data is removed afterwards and the record deleted. Tests to ensure the database
		/// is returning the same information written to it (see base class for test properties )
		<![CDATA[/// </summary>]]>		
		[Test(Description="Inserts a record into <xsl:value-of select="Name"/> and removes it")]
		public void InsertDeleteRecord ()
		{
		
			// Record the current amount <xsl:value-of select="Name"/> records in the database
			int records = <xsl:value-of select="HeaderInfo/ProjectName"/>.Dal.<xsl:value-of select="Name"/>.CountAll ();
		
			<xsl:value-of select="HeaderInfo/ProjectName"/>.Dal.<xsl:value-of select="Name"/> obj = new
				<xsl:value-of select="HeaderInfo/ProjectName"/>.Dal.<xsl:value-of select="Name"/> ();
			
			// Create a record and populate all properties
			BuildRecord ( obj );
			
			
			// Save the object which ( note if this class contains other ActiveRecord objects, saves may
			// have already occured )
			obj.Save ();
			
			
			// NUnit test that the amount of <xsl:value-of select="Name"/> records has increased
			Assert.IsTrue ( records+1<xsl:for-each select="ClassAttribs/ClassAttributes"><xsl:if test="Type=$Name">+1</xsl:if></xsl:for-each> == <xsl:value-of select="HeaderInfo/ProjectName"/>.Dal.<xsl:value-of select="Name"/>.CountAll () );
			
			
			
			
			// Reset the session scope - doing this will cause and new reads to occur from
			// the database and not from memory.
			base.Scope.Dispose ();
			base.Scope = new SessionScope ();
			
			// Reload the object by using the primary key
			obj = <xsl:value-of select="//HeaderInfo/ProjectName"/>.Dal.<xsl:value-of select="Name"/>.FindByPrimaryKey ( obj.<xsl:value-of select="Name"/>ID );
			
			// Ensure all properties are set to what was written 
			<xsl:for-each select="ClassAttribs/ClassAttributes[CType='single' and KnownClass='false' and IsEnum='false']"><xsl:sort select="PropertyName" order="descending"/>
			Assert.IsTrue ( obj.<xsl:value-of select="PropertyName"/>.Equals ( NUnitBase.Test<xsl:value-of select="Type"/> ),
				string.Format ( CultureInfo.CurrentCulture , "{0} != {1}" , obj.<xsl:value-of select="PropertyName"/>, NUnitBase.Test<xsl:value-of select="Type"/> ) );</xsl:for-each>
			
			<xsl:for-each select="ClassAttribs/ClassAttributes[CType='single' and KnownClass='true' and IsEnum='false']"><xsl:sort select="PropertyName" order="ascending"/>
			Assert.IsTrue ( obj.<xsl:value-of select="PropertyName"/>.<xsl:value-of select="Type"/>ID != Guid.Empty ,
				string.Format ( CultureInfo.CurrentCulture , "{0} == {1}" , obj.<xsl:value-of select="PropertyName"/>.<xsl:value-of select="Type"/>ID, Guid.Empty ) );
			</xsl:for-each>
			
			<xsl:for-each select="ClassAttribs/ClassAttributes[CType='array' and KnownClass='true' and IsEnum='false']"><xsl:sort select="PropertyName" order="ascending"/>
			foreach ( <xsl:value-of select="//HeaderInfo/ProjectName"/>.Dal.<xsl:value-of select="Type"/> arrayObject in obj.<xsl:value-of select="PropertyName"/> )
			{
				Assert.IsTrue ( arrayObject.<xsl:value-of select="Type"/>ID != Guid.Empty ,
				string.Format ( CultureInfo.CurrentCulture , "{0} == {1}" , arrayObject.<xsl:value-of select="Type"/>ID, Guid.Empty ) );
			}
			</xsl:for-each>



			// Delete all the test objects that we have created
			
			// Foreign key restraints mean we have to delete the main object before
			// removing any of the linked objects ( if any )
			obj.Delete ();
			
			
			<xsl:for-each select="ClassAttribs/ClassAttributes[CType='single' and KnownClass='true' and IsEnum='false']"><xsl:sort select="PropertyName" order="ascending"/>
			obj.<xsl:value-of select="PropertyName"/>.Delete ();
			</xsl:for-each>
			
			<xsl:for-each select="ClassAttribs/ClassAttributes[CType='array' and KnownClass='true' and IsEnum='false']"><xsl:sort select="PropertyName" order="ascending"/>
			foreach ( <xsl:value-of select="//HeaderInfo/ProjectName"/>.Dal.<xsl:value-of select="Type"/> arrayObject in obj.<xsl:value-of select="PropertyName"/> )
			{
				arrayObject.Delete ();
			}
			</xsl:for-each>
			
			
		
		}
		
		<![CDATA[/// <summary>
		/// Build a test record which will be used for writing and reading from the database
		/// </summary>
		/// <param name="obj"></param>]]>
		private static void BuildRecord ( <xsl:value-of select="HeaderInfo/ProjectName"/>.Dal.<xsl:value-of select="Name"/> obj )
		{
			<xsl:for-each select="ClassAttribs/ClassAttributes[CType='single' and KnownClass='false' and IsEnum='false']"><xsl:sort select="PropertyName" order="ascending"/>
			obj.<xsl:value-of select="PropertyName"/> = NUnitBase.Test<xsl:value-of select="Type"/>;</xsl:for-each>
			
			<xsl:for-each select="ClassAttribs/ClassAttributes[CType='single' and KnownClass='true' and IsEnum='false']"><xsl:sort select="PropertyName" order="ascending"/>
			obj.<xsl:value-of select="PropertyName"/> = new <xsl:value-of select="//HeaderInfo/ProjectName"/>.Dal.<xsl:value-of select="Type"/> ();
			obj.<xsl:value-of select="PropertyName"/>.Save ();
			</xsl:for-each>
			
			<xsl:for-each select="ClassAttribs/ClassAttributes[CType='array' and KnownClass='true' and IsEnum='false']"><xsl:sort select="PropertyName" order="ascending"/>
			<xsl:value-of select="//HeaderInfo/ProjectName"/>.Dal.<xsl:value-of select="Type"/> _<xsl:value-of select="Name"/>
				= new <xsl:value-of select="//HeaderInfo/ProjectName"/>.Dal.<xsl:value-of select="Type"/> ();
			_<xsl:value-of select="Name"/>.Save ();
			obj.<xsl:value-of select="PropertyName"/>.Add ( _<xsl:value-of select="Name"/> );
			</xsl:for-each>
		
		}
		
		
		
		#region NUnitSetup
		<![CDATA[/// <summary>
		/// Setup the ActiveRecord environment for this NUnit test (see base implementation)
		/// </summary>]]>
		[SetUp]
		public new void Setup ()
		{
			base.Setup();
		}
	
		<![CDATA[/// <summary>
		/// Shutdown the ActiveRecord environment for this NUnit test (see base implementation)
		/// </summary>]]>
		[TearDown]
		public new void Teardown ()
		{
			base.Teardown();
		}
		
		
		<![CDATA[/// <summary>
		/// Setup this test fixture by registering all ActiveRecord types within this Assembly.
		/// </summary>]]>
		[TestFixtureSetUp]
		public void TestFixtureSetup ()
		{

			List&lt;Type&gt; types = new List&lt;Type&gt;();
			
			foreach ( Type type in System.Reflection.Assembly.GetAssembly ( this.GetType () ).GetTypes () )
			{
				if ( type.BaseType == typeof ( <xsl:value-of select="HeaderInfo/ProjectName"/>.Dal.DataObject ) ) 
					types.Add ( type );
			}
			
			NUnitBase.TestFixtureSetup ( types.ToArray() );
		}
		
		
		#endregion
		
		
		
	}
	</xsl:if>





}
		

	</xsl:template>
	
	
	
</xsl:stylesheet>